package cn.lsnu.common.interfacetools.interfaceservice;

import cn.lsnu.common.interfacetools.interfacedao.TopDao;
import cn.lsnu.common.interfacetools.interfaceentity.BaseEntity;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
/**
 * @Description
 * @return null
 * @author  lin
 * @Datetime  2020/2/6 10:17
 */  
public abstract class BaseService<T extends BaseEntity<T>, D extends TopDao<T>>  implements  TopService<T>  {

    @Autowired
    protected D dao;

    @Override
    public void save(T entity) throws Exception {
       dao.save(entity);

    }

    @Override
    public T get(String key) throws Exception {
        return dao.get(key);
    }

    @Override
    public T getEntity(T entity) throws Exception {

        return dao.getEntity(entity);
    }

    @Override
    public List<T> findList(T entity) throws Exception {
        return dao.findList(entity);
    }

    public Page<T> findPage(T entity) throws Exception {
        Integer count = dao.count(entity);
        entity.getPage().setCount(count);
        Page<T> pages = entity.getPage();
        pages.setData(dao.findList(entity));
        pages.setCount(count);
        return pages;
    }

    @Override
    public void delete(T entity) throws Exception {
        dao.delete(entity);
    }
    @Override
    public void update(T entity) throws Exception {
        dao.update(entity);
    }
}
